package com.csun.dsms.task;

import com.csun.dsms.model.domain.DsmsSchedule;
import com.csun.dsms.service.DsmsRecordService;
import com.csun.dsms.service.DsmsScheduleLogService;
import com.csun.dsms.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-23 15:51
 * @description: TODO
 */
@Slf4j
@Service
public class ServiceScheduledTaskJob implements ScheduledTaskJob {

    private DsmsSchedule dsmsSchedule;

    @Resource
    private DsmsRecordService dsmsRecordService;

    @Resource
    private DsmsScheduleLogService dsmsScheduleLogService;

    @Override
    public void setSchedule(DsmsSchedule dsmsSchedule) {
        this.dsmsSchedule = dsmsSchedule;
    }

    @Override
    public void run() {
        Date startTime = new Date();

        log.info("=====> this service schedule run name = {}", Thread.currentThread().getName());

        // 自动生成待服务记录
        String date = DateUtil.getNowMonth();
        dsmsRecordService.autoCreate(date);

        Date endTime = new Date();
        dsmsScheduleLogService.insert(dsmsSchedule, startTime, endTime);
    }
}
